const state = {
    menus: [],
    submenus: []
}

/* eslint-disable */
// getters
const getters = {
    findHighlightMenu: (state)  => (path) => {
        return state.submenus.find(menu => menu.url === path)
    }
}
/* eslint-disable */

const actions = {
    refresh ({commit}, currentMenus) {
        commit('refreshMenus', currentMenus)
    },
    clear ({commit}) {
        commit('clearMenus')
    }
}

// mutations
const mutations = {
    refreshMenus (state, menus) {
        state.menus = menus
        // 构建菜单数组
        let sumenus = []
        state.menus.forEach(element => {
            if (element.children != undefined) {
                element.children.forEach(c_el => {
                    c_el.parentCode = element.code
                    c_el.parentTitle = element.title
                    sumenus.push(c_el)
                })
            }
        });
        state.submenus = sumenus
    },
    clearMenus (state) {
        state.menus = []
        state.submenus = []
    }
}

export default {
    namespaced: true,
    getters,
    state,
    actions,
    mutations
  }